//
//  WKApi.m
//  shengxian_delivery
//
//  Created by pg on 2017/3/4.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "WKApi.h"
#import "Config.h"
#import "UpdateToken.h"

@implementation WKApi

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.needHUD = YES;
        self.page = 1;
    }
    return self;
}

/*
 不需要传token 子类传nil
 */
-(NSMutableArray<id<YTKRequestAccessory>> *)requestAccessories
{
    NSMutableArray *arr = [NSMutableArray new];
    [arr addObject:[UpdateToken new]];
    return arr;
}

/*
 header传token
 */
-(NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary
{
    NSString *token = [Config Token];
    if (token) {
        return @{@"Authorization":token};
    }else{
//        [self stop];
        return nil;
    }
}

-(YTKRequestMethod)requestMethod
{
    return YTKRequestMethodPOST;
}

-(NSString *)baseUrl
{
    return @"https://api.haiguazi.cainiaowx.com/v1/counselor";
}

-(void)startWithCompletionBlockWithSuccess:(YTKRequestCompletionBlock)success failure:(YTKRequestCompletionBlock)failure
{
    [super startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
     
        NSLog(@"%@",request.responseJSONObject);
        if (self.needHUD) {
            if ([[request.responseJSONObject objectForKey:@"code"] intValue] != 0) {
                kShowError(request.responseJSONObject)
            }
        }
        if (success) {
            success(request);
        }
        
    }failure:^(__kindof YTKBaseRequest * _Nonnull request) {
 
        if ([request.error.localizedDescription isEqualToString:@"Request failed: unauthorized (401)"]) {
            //token 过期重新登录;
            [[NSNotificationCenter defaultCenter]postNotificationName:kLogOutNotification object:nil];
            [MBProgressHUD showError:@"请重新登录"];
        }else{
            kWangLuoLianJieBuChang
        }
        
        if (failure) {
            failure(request);
        }
    }];
}

-(void)dealloc
{
    NSLog(@"%@ dealloc",[self class]);
}


@end
